#!/usr/bin/env python3

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument, GroupAction, IncludeLaunchDescription
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
from launch_ros.substitutions import FindPackageShare
from launch.conditions import IfCondition, UnlessCondition
from launch.launch_description_sources import PythonLaunchDescriptionSource

def generate_launch_description():
    # 声明启动参数
    use_config_file_arg = DeclareLaunchArgument(
        'use_config_file',
        default_value='true',
        description='Whether to use config file for parameters'
    )

    namespace_arg = DeclareLaunchArgument(
        'namespace',
        default_value='demo_ns',
        description='Namespace for nodes'
    )

    # 创建带命名空间的节点组
    namespaced_nodes = GroupAction(
        actions=[
            Node(
                package='multi_launch_demo',
                executable='publisher_node',
                name='publisher_node',
                namespace=LaunchConfiguration('namespace'),
                parameters=[{
                    'publish_frequency': 1.5,
                    'message_prefix': 'Namespaced'
                }],
                output='screen'
            ),
            Node(
                package='multi_launch_demo',
                executable='subscriber_node',
                name='subscriber_node',
                namespace=LaunchConfiguration('namespace'),
                output='screen'
            )
        ]
    )

    # 创建服务节点
    service_server_node = Node(
        package='multi_launch_demo',
        executable='service_server_node',
        name='service_server_node',
        output='screen'
    )

    service_client_node = Node(
        package='multi_launch_demo',
        executable='service_client_node',
        name='service_client_node',
        output='screen'
    )

    # 使用配置文件的参数节点
    param_node_with_config = Node(
        package='multi_launch_demo',
        executable='param_node',
        name='param_node',
        parameters=[PathJoinSubstitution([
            FindPackageShare('multi_launch_demo'),
            'config',
            'demo_params.yaml'
        ])],
        output='screen',
        condition=IfCondition(LaunchConfiguration('use_config_file'))
    )

    # 不使用配置文件的参数节点
    param_node_without_config = Node(
        package='multi_launch_demo',
        executable='param_node',
        name='param_node',
        parameters=[{
            'param_string': 'Default value',
            'param_int': 50,
            'param_double': 1.414,
            'param_bool': True
        }],
        output='screen',
        condition=UnlessCondition(LaunchConfiguration('use_config_file'))
    )

    return LaunchDescription([
        use_config_file_arg,
        namespace_arg,
        namespaced_nodes,
        service_server_node,
        service_client_node,
        param_node_with_config,
        param_node_without_config
    ])
